package com.study.springdemo2.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;


@RestController
@Tag(name = "SpringDemo2")
@RequestMapping("/springdemo2")
public class PromptController {
    @Autowired
    @Qualifier("openAIClientDeepSeek")
    private ChatClient openAIClientDeepSeek;

    @Resource(name = "deepseekModel")
    private ChatModel deepseekModel;
    @GetMapping("/prompt1")
    public Flux<String> chat(String question)
    {
        return openAIClientDeepSeek.prompt()
                // AI 能力边界
                .system("你是一个法律助手，只回答法律问题，其它问题回复，我只能回答法律相关问题，其它无可奉告")
                .user(question)
                .stream()
                .content();
    }

    @GetMapping("/prompt2")
    public Flux<String> chat2(String question) {
        return openAIClientDeepSeek.prompt()
                // 系统角色说明
                .system("你是一个讲故事的助手，每个故事控制在300字以内")
                // 用户输入
                .user(question)
                // 启动流式对话
                .stream()
                // 获取输出内容
                .content();
    }

    @GetMapping("/prompt3")
    public Flux<String> chat3(String question)
    {
        String prompt = "你是一个讲故事的助手,每个故事控制在600字以内且以HTML格式返回";
        return openAIClientDeepSeek.prompt().
                system(prompt).
                user(question)
                // 启动流式对话
                .stream()
                // 获取输出内容
                .content();
    }

    @GetMapping("/prompt4")
    public String chat4(String city)
    {
        String answer = openAIClientDeepSeek.prompt()
                .user(city + "未来3天天气情况如何?")
                .call()
                .chatResponse()
                .getResult()
                .getOutput()
                .getText();

        ToolResponseMessage toolResponseMessage =
                new ToolResponseMessage(
                        List.of(new ToolResponseMessage.ToolResponse("1","获得天气",city)
                        )
                );
        String toolResponse = toolResponseMessage.getText();
        String result = answer + toolResponse;
        return result;
    }
}
